<template>
  <div class="comment">
    <div class="heads">
      <div class="fh" @click="$router.push({ path: '/Home' })">
        <span class="iconfont icon-arrow-left-bold"></span> 返回
      </div>
      <div class="center">全部评论</div>
      <router-link class="shuJ" tag="div" to="/bookshelf"
        ><span class="iconfont icon-shujia"></span> 书架</router-link
      >
    </div>
    <div class="item" v-for="item in Comments" :key="item.commentId">
      <div class="img">
        <img :src="item.avatar" alt="" width="100%" height="100%" />
      </div>
      <div class="title">
        <div class="name-time">
          <div class="name">{{ item.nickName }}</div>
          <div class="time">{{ item.time | time }}</div>
        </div>
        <!-- <div class="stp"></div> -->
        <div class="text">
          {{ item.comment }}
        </div>
        <div class="z">
          <span class="iconfont icon-dayizan" @click="adds"></span
          ><span class="num">{{ item.likes }}</span>
        </div>
      </div>
    </div>
    <div class="bottom">
        <input type="text" name="" id="" placeholder="评论一下" v-model="txt">
        <span class="btn" @click="addComment">发送</span>
    </div>
  </div>
</template>

<script>
import { getComments } from "../api/details";
export default {
  data() {
    return {
      Comments: null,
      txt:''
    };
  },
  methods: {
    adds(e) {
      // console.log(e.target.nextElementSibling);
      e.target.classList.toggle("con");
      if (e.target.classList.contains("con")) {
        e.target.nextElementSibling.innerHTML =
          +e.target.nextElementSibling.innerHTML + 1;
      } else {
        e.target.nextElementSibling.innerHTML =
          +e.target.nextElementSibling.innerHTML - 1;
      }
    },
    addComment(){
        let token=window.localStorage.getItem('token');
        if(token){
            this.Comments.push({
                avatar:'https://img01.yzcdn.cn/vant/cat.jpeg',
                nickName:token,
                time:new Date(),
                comment:this.txt,
                likes:0
            })
        }else{
            this.$router.push({path:'/login'})
        }
        this.txt=''
    }
  },
  filters: {
    time(value) {
      var date = new Date(value); //时间戳为10位需*1000，时间戳为13位的话不需乘1000

      let Y = date.getFullYear() + "年";

      let M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "月";

      let D = date.getDate() + "日";

      return Y + M + D; //时分秒可以根据自己的需求加上
    },
  },
  created() {
    let id = this.$route.query.id;
    getComments({ id }).then((data) => {
      this.Comments = data.all.list;
      console.log(data);
    });
  },
};
</script>

<style lang="less">
.comment {
  position: relative;
  padding-bottom: 44px;
  .bottom {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 44px;
    background-color: #c8c9cc;
    border-top: 1px solid #8d8e900f;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    box-shadow: 0px 0px 3px #ccc;
    input{
        height: 100%;
        border: none;
        width: 80%;
        background-color: #ffffffde;
        padding-left: 15px;
    }
    .btn{
        position: absolute;
        right: 15px;
        color: rgb(117,117,117);
        height: 44px;
        line-height: 44px;
    }
  }
  .heads {
    display: flex;
    justify-content: space-between;
    color: #fff;
    background-color: black;
    height: 50px;
    line-height: 50px;
    .center {
      font-size: 20px;
    }
  }
  .item {
    display: flex;
    margin-top: 10px;
    border-bottom: 1px dotted #ccc;
    padding: 10px;
    &:last-of-type {
      border: none;
    }
    .img {
      width: 50px;
      height: 50px;
      background-color: pink;
      border-radius: 50%;
      margin-right: 10px;
      flex-shrink: 0;
      overflow: hidden;
    }
    .title {
      width: 100%;
      div {
        margin-bottom: 8px;
      }
      .name-time {
        display: flex;
        justify-content: space-between;
        .time {
          font-size: 13px;
          color: #a8a8a8;
        }
      }
      .z {
        text-align: end;
        .iconfont {
          &.con {
            color: rgb(235, 79, 79);
          }
        }
        .num {
          margin-left: 12px;
        }
      }
    }
  }
}
</style>